﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using StreetFinderDB;
using UnicodeConverter;
using StreetFinder;
using DataAccess;

/// <summary>
/// Summary description for GetAddress
/// </summary>
public class GetAddress
{
	public GetAddress()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string GetGeoInfoByPoint(string X, string Y, int nLg)
    {
        try
        {
            if (X == "0" || Y == "0" || X == "0.0" || Y == "0.0")
            {
                return "";
            }
            Converter converter = new Converter();
            GeoReturnAddressInfo geoinfo = GetGeoObjectByPoint(X, Y);
            //GeoReturnAddressInfo geoinfo = GetGeoObjectByPoint("106.699465", "10.7615083333333");
            if (geoinfo ==null)
            {
                return "";
            }
            string strStreet = geoinfo.szStreet;
            string strWard = geoinfo.szWard;
            string strDistrict = geoinfo.szDistrict;
            string strProvince = geoinfo.szProvince;

            if (nLg == 1)
            {
                if (strStreet != "")
                {
                    strStreet = strStreet.Replace("Đường ", "");
                    strStreet = strStreet.Replace("Phố ", "");
                    strStreet = strStreet.Replace("Tỉnh Lộ ", "");
                    strStreet = strStreet.Replace("Hẻm", "Lane");
                    if (IsNumber(strStreet))
                    {
                        strStreet = "Street " + strStreet;
                    }
                    else
                    {
                        strStreet = converter.TrimSign(strStreet);
                        strStreet += " Street";
                    }
                }

                if (strWard != "")
                {
                    if (strWard.IndexOf("Phường ") != -1)
                    {
                        strWard = strWard.Replace("Phường ", "");
                        if (IsNumber(strWard))
                        {
                            strWard = "Ward " + strWard;
                        }
                        else
                        {
                            strWard = converter.TrimSign(strWard);
                            strWard += " Ward";
                        }
                    }
                    else if (strWard.IndexOf("Thị Trấn ") != -1)
                    {
                        strWard = strWard.Replace("Thị Trấn ", "");
                        strWard = converter.TrimSign(strWard);
                        strWard += " Town";
                    }
                    else
                    {
                        strWard = strWard.Replace("Xã ", "");
                        strWard = converter.TrimSign(strWard);
                        strWard += " Commune";
                    }
                }

                if (strDistrict != "")
                {
                    if (strDistrict.IndexOf("Quận ") != -1)
                    {
                        strDistrict = strDistrict.Replace("Quận ", "");
                        if (IsNumber(strDistrict))
                        {
                            strDistrict = "District " + strDistrict;
                        }
                        else
                        {
                            strDistrict = converter.TrimSign(strDistrict);
                            strDistrict += " District";
                        }
                    }
                    else if (strDistrict.IndexOf("Thành Phố ") != -1)
                    {
                        strDistrict = strDistrict.Replace("Thành Phố ", "");
                        strDistrict = converter.TrimSign(strDistrict);
                        strDistrict += " City";
                    }
                    else
                    {
                        strDistrict = strDistrict.Replace("Huyện ", "");
                        strDistrict = converter.TrimSign(strDistrict);
                        strDistrict += " District";
                    }
                }

                if (strProvince != "")
                {
                    if (strProvince.IndexOf("Thành Phố ") != -1 || strProvince.IndexOf("Thủ Đô ") != -1)
                    {
                        strProvince = strProvince.Replace("Thành Phố ", "");
                        strProvince = strProvince.Replace("Thủ Đô ", "");
                        strProvince = converter.TrimSign(strProvince);
                        strProvince += " City";
                    }
                    else
                    {
                        strProvince = strProvince.Replace("Tỉnh ", "");
                        strProvince = converter.TrimSign(strProvince);
                        strProvince += " Province";
                    }
                }
            }

            string address = strStreet + ", " + strWard + ", " + strDistrict + ", " + strProvince;

            return address.Trim(new char[] { ',', ' ' });
        }
        catch
        { return ""; }
    }

    public bool IsNumber(string pText)
    {
        pText = pText.Trim();
        Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
        return regex.IsMatch(pText);
    }

    public GeoReturnAddressInfo GetGeoObjectByPoint(string X, string Y)
    {
        try
        {
            CStreetFinder pro = new CStreetFinder();
            GeoReturnAddressInfo geoinfo = pro.GetGeoInfo(double.Parse(X), double.Parse(Y), 100.0);

            if (true) //(geoinfo.nSuccess != GeoReturnAddressStatus.NotFound)
            {
                if (geoinfo.szStreet == "Ñöôøng Khoâng Teân" || geoinfo.szStreet == null) geoinfo.szStreet = "";
                else
                {
                    string[] AddrToken = geoinfo.szStreet.Split(' ');

                    if ((AddrToken[0] == "Heûm" || AddrToken[0] == "Ngoõ") && AddrToken.Length > 2)
                    {
                        //if (new GBObjectProvider().IsAddressNumber(AddrToken[1]) != "")
                        {
                            if (AddrToken[2] != "Ñöôøng")
                                geoinfo.szStreet = (AddrToken[0] == "Ngoõ") ? "" : "Ñöôøng";
                            else
                                geoinfo.szStreet = "";

                            for (int i = 2; i < AddrToken.Length; i++)
                                geoinfo.szStreet += " " + AddrToken[i];
                        }
                    }
                }
                //Converter convert = new Converter();
                //geoinfo.szStreet = convert.VNI2Unicode(geoinfo.szStreet);
                //geoinfo.szWard = convert.VNI2Unicode(geoinfo.szWard);
                //geoinfo.szDistrict = convert.VNI2Unicode(geoinfo.szDistrict);
                //geoinfo.szProvince = convert.VNI2Unicode(geoinfo.szProvince);
            }
            return geoinfo;
        }
        catch
        { return null; }
    }
}